package com.test05_数组应用;

/**
 * 多对多关联映射
 */
public class YootkDemo_04 {

    public static void main(String[] args) {
        // 声明并实例化各自的类对象
        User userA = new User("110150130", "张三", "www.yootk.com", 0);
        User userB = new User("120150130", "李四", "www.yootk.com", 0);
        User userC = new User("130150130", "王五", "www.yootk.com", 0);
        Role roleA = new Role(1, "用户管理", "yootk.ke.qq.com");
        Role roleB = new Role(2, "系统维护", "yootk.ke.qq.com");
        Action actionA = new Action(10, "创建用户", "edu.yootk.com");
        Action actionB = new Action(11, "编辑用户", "edu.yootk.com");
        Action actionC = new Action(12, "系统初始化", "edu.yootk.com");
        Action actionD = new Action(13, "系统备份", "edu.yootk.com");
        Action actionE = new Action(14, "首页静态化", "edu.yootk.com");
        Action actionF = new Action(15, "数据迁移", "edu.yootk.com");
        // 设置彼此之间的引用关联
        roleA.setActions(new Action[]{actionA, actionB});
        roleB.setActions(new Action[]{actionC, actionD, actionE, actionF});
        actionA.setRole(roleA);
        actionB.setRole(roleA);
        actionC.setRole(roleB);
        actionD.setRole(roleB);
        actionE.setRole(roleB);
        actionF.setRole(roleB);
        userA.setRoles(new Role[]{roleA});
        userB.setRoles(new Role[]{roleB});
        userC.setRoles(new Role[]{roleA, roleB});
        roleA.setUsers(new User[]{userA, userC});
        roleB.setUsers(new User[]{userB, userC});
        // 依据引用结构获取相关的数据信息
        System.out.println("--------------------- 根据用户获取角色和权限信息 --------------------------");
        System.out.println(userC.getInfo());
        for (int x = 0; x < userC.getRoles().length; x++) {
            System.out.println("\t|- " + userC.getRoles()[x].getInfo());
            for (int y = 0; y < userC.getRoles()[x].getActions().length; y++) {
                System.out.println("\t\t|- " + userC.getRoles()[x].getActions()[y].getInfo());
            }
        }
        System.out.println("\n--------------------- 根据角色获取用户和权限信息 --------------------------");
        System.out.println(roleA.getInfo());
        for (int x = 0; x < roleA.getUsers().length; x++) {
            System.out.println("\t|- " + roleA.getUsers()[x].getInfo());
        }
        for (int x = 0; x < roleA.getActions().length; x++) {
            System.out.println("\t|- " + roleA.getActions()[x].getInfo());
        }
        System.out.println("\n--------------------- 根据权限获取角色以及拥有此权限的用户 --------------------------");
        System.out.println(actionA.getInfo());
        System.out.println("\t|- " + actionA.getRole().getInfo());
        for (int x = 0; x < actionA.getRole().getUsers().length; x++) {
            System.out.println("\t\t|- " + actionA.getRole().getUsers()[x].getInfo());
        }
    }
}

/**
 * 用户信息
 */
class User {
    private String phone;
    private String name;
    private String password;
    private int lock;
    private Role roles[];   // 用户拥有的角色

    public User() {
    }

    public User(String phone, String name, String password, int lock) {
        this.phone = phone;
        this.name = name;
        this.password = password;
        this.lock = lock;
    }

    public void setRoles(Role[] roles) {
        this.roles = roles;
    }

    public Role[] getRoles() {
        return this.roles;
    }

    public String getInfo() {
        return "【User】电话：" + this.phone + "、姓名：" + this.name + "、密码：" + this.password + "、锁定状态：" + this.lock;
    }
}

/**
 * 角色
 */
class Role {
    private long rid;
    private String title;
    private String note;
    private User users[];       // 拥有角色的用户
    private Action actions[];   // 角色拥有的权限

    public Role() {
    }

    public Role(long rid, String title, String note) {
        this.rid = rid;
        this.title = title;
        this.note = note;
    }

    public void setUsers(User[] users) {
        this.users = users;
    }

    public User[] getUsers() {
        return this.users;
    }

    public void setActions(Action actions[]) {
        this.actions = actions;
    }

    public Action[] getActions() {
        return this.actions;
    }

    public String getInfo() {
        return "【Role】角色ID：" + this.rid + "、角色名称：" + this.title + "、角色描述：" + this.note;
    }
}

/**
 * 权限
 */
class Action {
    private long aid;
    private String title;
    private String note;
    private Role role;      // 对应的角色

    public Action() {
    }

    public Action(long aid, String title, String note) {
        this.aid = aid;
        this.title = title;
        this.note = note;
    }

    public void setRole(Role role) {
        this.role = role;
    }

    public Role getRole() {
        return this.role;
    }

    public String getInfo() {
        return "【Action】权限ID：" + this.aid + "、权限名称：" + this.title + "、权限描述：" + this.note;
    }
}
